home *** CD-ROM | disk | FTP | other *** search
- …ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
- ∫ Spin Demo Information ∫
- ∫ July 20, 1994 ∫
- ∫ Peter M. Freese ∫
- »ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº
-
-
- Running Spin
- ~~~~~~~~~~~~
- Use left Ctrl and Alt to scale in and out, use right Ctrl and Alt to
- spin the bitmap.
-
- You can change the bitmap by replaced SPIN.PCX with your own image.
- The image must be a 64x64 bitmap.
-
-
- How does Spin work?
- ~~~~~~~~~~~~~~~~~~~
- Spin achieves very fast dynamic bitmap scaling and rotation by using a
- destination centric copy in a tightly coded assembly inner loop. By
- "destination centric", I mean that rather than determine where each
- pixel in the source bitmaps should go, it looks at each point in the
- destination and determines where the pixel comes from.
-
- The algorithm basically needs three bits of information in order to
- draw the scaled and rotated bitmap: the starting texture coordinate to
- draw at the upper left of the destination (startingU, startingV), the
- texture deltas to move right through the destination (dU, dV), and the
- texture deltas to move down through the destination (dU2, dV2).
-
- The algorithm works basically as follows:
-
- // calculate deltas
- dU = cos(angle) * scale;
- dV = sin(angle) * scale;
-
- dU2 = cos(angle - 90) * scale;
- dV2 = cos(angle - 90) * scale;
-
- rowU = startingU;
- rowV = startingV;
-
- for ( y = 0; y < height; y++)
- {
- u = rowU;
- v = rowV;
-
- for ( x = 0; x < width; x++)
- {
- dest[x,y] = texture[u,v];
- u += dU;
- v += dV;
- }
-
- rowU += dU2;
- rowV += dV2;
- }
-
- This is a simplified version of the algorithm, some details are not
- shown in order to make this pseudo code easier to understand:
-
- The startingU and startingV values are calculated from the distance
- from the center of the rotation to the upper left of the destination by
- using the delta values.
-
- The vertical delta are aspect adjusted for VGA mode 0x13.
-
- The texture coordinates are all implemented as fixed point numbers.
- The radix points are set so that texture coordinates overflow (wrap) at
- the edges of the source texture. This gives the most precision, and
- speeds the wrapping. In order for this to work, the size of the source
- texture must be a power of two. In the case of the example, I used a
- 64x64 texture.
-
- If you understand this algorithm, you should see that the scaling
- process is almost happenstance -- the deltas are merely adjusted by a
- factor. If you just want to do scaling without rotation, you should be
- able to greatly simplify this algorithm.
-
- If you are wondering how tightly this algorithm can be coded in
- assembly, the Spin inner loop uses just 6 instructions per pixel, not
- counting the destination write (the inner loop is unrolled to write 4
- pixels at a time). You should be able to do just scaling in less.
-
-
- 486 cache misses
- ~~~~~~~~~~~~~~~~
- The spin speed suffers when dealing with large bitmaps. The problem is
- that when the bitmap is larger than the CPU cache (8K on a 486), there
- will be cache misses while drawing the bitmap. This is aggravated when
- the bitmap is rotated near 90 or 270 degrees, since there will likely
- be many cache misses over the course of a scan line. Theoretically,
- this problem can be minimized by using a version of the image already
- rotated by 90 degrees for certain quadrants of rotation. Ultimately,
- however, even this will be of little help as the image gets larger than
- 256x256. How much of an impact do cache misses have? Run SPIN and
- turn of your 486 CPU's internal cache (most 486 turbo buttons don't
- actually change the CPU speed, but just disable the cache). On my
- machine the frame rate drops from 90 to about 16.
-
- Unfortunately, there's not much you can do about cache misses except be
- aware of their consequence and code accordingly.
-
-
- More questions?
- ~~~~~~~~~~~~~~~
- If you have any further questions about how Spin works, post me a
- message in the Game Design section of the Gamers forum. I prefer a
- forum message rather than private email since others will get the
- benefit of the reply.
-
- Thanks for your feedback.
-
- Peter Freese
- 74170,543 (CompuServe)
- codezero@halcyon.com
-